package com.ozo.boot.common.database.util;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ozo.boot.common.base.model.BaseSortable;
import com.ozo.boot.common.base.util.NamingConventionUtils;

/**
 * 排序工具类
 *
 * @author qiangesoft
 * @date 2024-09-05
 */
public class SortUtils {

    private SortUtils() {
    }

    /**
     * 设置排序条件
     * @param queryWrapper
     * @param prop
     * @param order
     * @param <T>
     */
    public static <T> void setSortCondition(QueryWrapper<T> queryWrapper, String prop, String order) {
        if (StrUtil.isNotBlank(prop)) {
            String underlineProp = NamingConventionUtils.camelToUnderline(prop);
            if (BaseSortable.ASC.equals(order)) {
                queryWrapper.orderByAsc(underlineProp);
            } else {
                queryWrapper.orderByDesc(underlineProp);
            }
        }
    }

}
